package org.darkforest.merchant.controller;

import org.darkforest.common.services.MansongService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Utils;
import org.darkforest.dao.mapper.GoodsClassMapper;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.GoodsClass;
import org.darkforest.model.PMansong;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * 满即送活动控制类
 * Created by Administrator on 2016/1/12.
 */

@Controller
@RequestMapping("promotion")
public class RewarForGiftsController {


    @Autowired
    private MansongService mansongService;


    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 满即送活动分页列表
     * @param session
     * @param p  分页中的当前页
     * @param state 满即送活动状态
     * @param mansongName 满即送活动名称
     * @return
     */
    @RequestMapping(value = "rewardForGiftsList")
    @AuthPassport
    public ModelAndView list(HttpSession session,
                             @RequestParam(required = false, defaultValue = "1") String p,
                             @RequestParam(required = false, defaultValue = "") String state,
                             @RequestParam(required = false, defaultValue = "") String mansongName){
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        ModelAndView res = new ModelAndView("promotion/mansong/mansong_list");
        PMansong pMansong = new PMansong();
        pMansong.setStart(new Integer(p));
        pMansong.setStoreId(storeId);
        pMansong.setMansongName("".equals(mansongName)?null:mansongName);
        pMansong.setState("".equals(state) ? null : new Byte(state));
        pMansong.setResultList(mansongService.findListByCondition(pMansong));
        res.addObject("pageBean",pMansong);
        return  res;
    }

    /**
     * 跳转到新增满送活动页面
     * @return
     */
    @RequestMapping(value = "toAddMansong")
    public ModelAndView toAddMansong(HttpSession session){
        StoreSellerVo store = ((StoreSellerVo) session.getAttribute("sellerInfo"));
        ModelAndView res = new ModelAndView("promotion/mansong/mansong_add");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        PMansong pMansong = new PMansong();
        pMansong.setStoreId(store.getStoreId());
//        pMansong.setState((byte)1);
        pMansong.setNowTime(DateUtil.unixTimeStampInt().intValue());
//        List<PMansong> pMansongList = mansongService.findListByCondition(pMansong);

//        if(pMansongList.size()>=1){
//            res.addObject("today",pMansongList.get(0).getEndTimeDes());
//        }else{
            res.addObject("today",sdf.format(new Date()));
//        }
        List<GoodsClass> goodsClassListAll = new ArrayList<GoodsClass>();

        List<GoodsClass> level1Gc = goodsClassMapper.findGoodsClass(new GoodsClass(0, false));
        for (GoodsClass goodsClass1 : level1Gc) {
            goodsClassListAll.add(goodsClass1);
//                List<GoodsClass> level2Gc =goodsClassRedisService.getGoodsClassList(goodsClass1.getGcId());
            List<GoodsClass> level2Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass1.getGcId()));
            goodsClassListAll.addAll(level2Gc);
            for (GoodsClass goodsClass2 : level2Gc) {
                goodsClass2.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass2.getGcName());
                List<GoodsClass> level3Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass2.getGcId()));
                goodsClassListAll.addAll(level3Gc);
                for(GoodsClass goodsClass3 : level3Gc){
                    goodsClass3.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass3.getGcName());
                }
            }
        }

        res.addObject("goodsClassList",goodsClassListAll);

        return res;
    }


    /**
     * 执行添加满即送活动
     * @param pMansong
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "doAddMansong")
    public @ResponseBody
    AjaxReturnData doAddMansong(PMansong pMansong,HttpSession session) throws Exception {
        StoreSellerVo store = ((StoreSellerVo) session.getAttribute("sellerInfo"));
        pMansong.setMemberId(store.getMemberId());
        pMansong.setMemberName(store.getMemberName());
        pMansong.setStoreId(store.getStoreId());
        pMansong.setStoreName(store.getStoreName());
        pMansong.setState((byte)0);
        String url ="promotion/rewardForGiftsList";
        AjaxReturnData ajaxReturnData = mansongService.addManSongActive(pMansong,url);
        return ajaxReturnData;
    }


    /**
     * 删除满即送活动 根据ID
     * @param mansongId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delMansong")
    public @ResponseBody AjaxReturnData delMansong(Integer mansongId ) throws Exception {
        String url = "promotion/rewardForGiftsList";
        AjaxReturnData ajaxReturnData = mansongService.deleMansongActive(mansongId,url);
        return ajaxReturnData;
    }




    @RequestMapping(value = "mansongDetail")
    public ModelAndView mansongDetail(Integer mansongId){
        ModelAndView res = new ModelAndView("promotion/mansong/mansong_detail");
        res.addObject("mansong",mansongService.getPMansongWithRules(mansongId));
        return res;
    }



    /**
     * 添加满即送活动时 选择满即送商品
     * @param request
     * @param goodsName
     * @param start
     * @return
     */
    @RequestMapping("selectMansongGoods")
    public ModelAndView selectMansongGoods(HttpServletRequest request,
                                           @RequestParam(required = false, defaultValue = "") String goodsName,
                                           @RequestParam(required = false, defaultValue = "1") String start
    ){
        ModelAndView modelAndView = new ModelAndView("promotion/mansong/select_mansong_goods");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        GoodSearchBean goodSearchBean = new GoodSearchBean();
        goodSearchBean.setGoodsName(Utils.encodeGetParam(goodsName));
        goodSearchBean.setStoreId(sellerVo.getStoreId());
        goodSearchBean.setStart(Integer.parseInt(start));
        goodSearchBean.setPageSize(8);
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setGoodsState(1);
        goodSearchBean.setGoodsVerify(1);
        this.goodsService.searchGoodsByPage(goodSearchBean);
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }




}
